<?php

class CPasswordChange extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;
	
	protected $member_type;

	public function __construct($member_type){
		
		$this->_name=R_PRE."password_change";
		
		$this->_primary="changeid";
		
		$this->member_type=$member_type;
		
		parent::__construct();
		
	}

	public function createPasswordChange($data){
		
		$changeid=$this->insert($data);
		
		return $changeid;
		
	}
	
	public function editPasswordChange($changeid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('changeid = ?', $changeid);
		
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
		
	}
	
	public function getDetails($fieldArray=null){
		
		$db=$this->getAdapter();
		
		$whereString=null;
		
		if($fieldArray!=null){
		
			$where=array();
			
			foreach ($fieldArray as $field => $value){
				
				$where[]= $db->quoteInto("$field = ?", $value);
				
			}
			
			$whereString=join(' AND ',$where);
		
		}
		
		$row=$this->fetchAll($whereString);
		
		if($row!=null){
	
			return $row->toArray();
			
       	}else{
       		
       		return array();
       		
       	}
		
	}
	
	public function sendMail($userEmail,$username,$newPassword,$random_str){
		
		if($this->member_type=='buyer'){
		
			$this->sendMailBuyer($userEmail,$username,$newPassword,$random_str);
		
		}else{
			
			$this->sendMailSeller($userEmail,$username,$newPassword,$random_str);
			
		}
			
	}

	public function sendMailBuyer($userEmail,$username,$newPassword,$random_str){
		
		$mail=new CMail();
			
		$subject='Password change notice';
		
		Zend_Loader::loadClass('CString');
		
		$content='Your password has been changed.';
		
		$content.='<br />Login Email:&nbsp;'.$username;
		
		$content.='<br />Password:&nbsp;'.$newPassword;
		
		$content.='<br />Please click this link to confirm:';
		
		//$content.='<br />Please click here to confirm: ';
		
		$content.='<br /><a target="blank" href="'.MY_URL.'/buyer-password-confirm?random_str='.$random_str.'">'.MY_URL.'/buyer-password-confirm?random_str='.$random_str.'</a>';
		
		$mail->sendEmail(CDS_EMAIL_NAME,$userEmail,$subject,$content);
		
	}
	
	public function sendMailSeller($userEmail,$username,$newPassword,$random_str){
		
		$mail=new CMail();
			
		$subject='忘记密码，密码改变通知';
		
		Zend_Loader::loadClass('CString');
		
		$content='您的密码已经修改了！';
		
		$content.='<br />登录名称: '.$username;
		
		$content.='<br />密码: '.$newPassword;
		
		$content.='<br />点击这里确认激活新密码：<a target="blank" href="'.MY_URL.'/seller-password-confirm?random_str='.$random_str.'">'.MY_URL.'/seller-password-confirm?random_str='.$random_str.'</a>';
		
		$mail->sendEmail(CDS_EMAIL_NAME,$userEmail,$subject,$content);
		
	}
	
}